import React, { Component } from 'react';
import { Divider, Popconfirm, Switch, Tag, Modal,Checkbox} from 'antd';
import { createList } from '/common/components/HocCRUD';
import { map, isObject } from 'lodash';
import MainWrap from '/common/components/HocCRUD/MainWrap';
import * as config from './config';
import { actionFunc } from '@/pages/Host';
import SwitchComp from '/common/componentsTpl/SwitchComp';
import ButtonComp from '/common/componentsTpl/ButtonComp';
import { resetPsw } from './service';
import ObjectBox from '/common/componentsTpl/ObjectBox';
import { appConfig } from '@/appConfig';
import SearchForm from '@/pages/Employee/SearchForm';
import { PageHeader } from 'ant-design-pro';


@createList({
  config,
  initSearchData: (props, data) => {
    return {
      ...data,
      fuzzyMatch: '',
    };
  },
  initFilterData: null,
})
export default class Employee extends Component {
  state = {
    password:""
  }
  /**
   * 状态开关
   * @param checked
   * @param record
   */
  // onChange(e) {
    // console.log(e,'this.props.searchDat');
    // // console.log(`checked =`, e,this.props);
    // let search = e&&e.name||e&&e.code;
    // if(search){
    //   this.props.searchData.search= search;
    // }else{
    //   this.props.searchData.search=''
    // }
    // this.props.fetchList();

  // }
  handleMoreSearch(values){
    this.props.setSearchData(values)
    this.props.fetchList();
  };
  toggleState(checked, record) {
    const { dispatch } = this.props;
    dispatch({
      type: 'employee/toggleState',
      payload: { id: record.id },
    });
  }
  // toggleISMake(checked, record) {
  //   const { dispatch } = this.props;
  //   dispatch({
  //     type: 'employee/toggleISMake',
  //     payload: { id: record.id },
  //   });
  // }
  resetPassword(record){
      const data = {
        id:record.id
      };
      resetPsw(data)
      .then(res=>{
          const {data,msg,status} = res;
          if(status==200 || status == 1){
             this.setState({
              password:data
             })
             Modal.success({
              title: msg+" !  您的新密码为：",
              content: this.state.password,
            });
          }else{
            Modal.error({
              title: "服务器请求错误！",
              content: "请稍后再请求......",
            });
          }
      })
  }

  render() {
    const headerTitle = (
      <SearchForm
        defaultOpen={this.state.isShowMore}
        formData={{ ...this.searchFormData }}
        onOk={(values, originalValues) => {
          this.searchFormData = originalValues;
          this.handleMoreSearch(values);
        }}
      />
    );
    const tableProps = {
      columns: [
        {
          title: '序号',
          dataIndex: 'no',
          key: 'no',
        },
        {
          title: '员工编码',
          dataIndex: 'code',
          key: 'code',
        },

        {
          title: '员工名称',
          dataIndex: 'name',
          key: 'name',
        },

        {
          title: '电话',
          dataIndex: 'phone',
          key: 'phone',
        },
        {
          title: '员工类型',
          dataIndex: 'type.name',
          key: 'type',
        },
        {
          title: '角色权限',
          dataIndex: 'roles.name',
          key: 'roles.code',
        },
        {
          title: '技术负责人',
          dataIndex: 'leader',
          key: 'leader',
          width: 180,
          render: (text) => {
            return (
              text===true?'是':'否'
            );
          },
        },
        {
          title: '所属部门',
          dataIndex: 'dept',
          key: 'dept',
          render: (text) => {
            return (
              isObject(text) ? (text.name) : ''
            );
          },
        },
        {
          title: '是否启用',
          dataIndex: 'enabled',
          key: 'enabled',
          width: 180,
          render: (text) => {
               return (
                 text===true?'开':'关'
               );
          },
        },
        {
          title: '操作',
          key: 'action',
          width: 250,
          render: (text, record) => (
          <>
          {
            actionFunc(this.props, text, record)
          }
          <Divider type='vertical'/>
            <Popconfirm
              title="确认重置密码吗?"
              onConfirm={()=>this.resetPassword(record)}
              okText="确认"
              cancelText="取消"
            >
            <ButtonComp btnType='a' actionType="resetPassword" children={'重置密码'}></ButtonComp>
            </Popconfirm>
          </>
          ),
        },
      ],
    };
    return (
      <MainWrap
        title={(
          <div style={{display:'flex'}}>
            {/*<ObjectBox style={{width:'200px'}} appCode={appConfig.EMPLOYEE.finderCode}/>*/}
           {headerTitle}
          </div>
        )}
        {...this.props}
        tableProps={tableProps}
        headActionOption = {{search:false,export:false,import:false}}
      >
        {
          this.props.children
        }
      </MainWrap>
    );
  }
}
